<?php
class info {

	function menulist($data){
		global $conf;
		$db = new db($conf);
		$lx = isset($data['id'])?intval($data['id']):0;
		if($lx==1 || $lx==2 || $lx==3){
			$list = $db->field(array("*"))->where("lx=$lx")->select("sc_menu");
			$str = "";
			for($i=0;$i<count($list);$i++){
				$str = $str."<option value=".$list[$i]['Id'].">".$list[$i]['name']."</option>";
			}
			echo json_encode(array(1,$str));
		}
	}
	
	function menuadd($data){
		global $conf;
		$db = new db($conf);
		$name = isset($data['t0'])?trim($data['t0']):"";
		$dir = isset($data['t1'])?trim($data['t1']):"";
		$menu = isset($data['t2'])?intval($data['t2']):0;
		$px = isset($data['t3'])?intval($data['t3']):0;
		$lx = isset($data['t4'])?intval($data['t4']):1;
		if($lx>3 || $lx<1){ error("栏目类型错误");}
		if(empty($name)){  error("请输入栏目名称");}
		if(empty($dir)){  error("请输入栏目目录");}
		$preg = "/\b[a-z]+\b/";
		if(!preg_match($preg,$dir)){ error("栏目目录请输入小写字母");}
		$r1 = $db->field(array("count(*)"))->where(array("dirname"=>"'".$dir."'"))->select("sc_menu");
		if($r1[0]['count(*)']!=0){ error("该目录已存在");}
		$field = array("name"=>$name,"dirname"=>$dir,"menu"=>$menu,"lx"=>$lx,"px"=>$px);
		$result = $db->insert("sc_menu",$field);
		$str = $result?array(1,"添加成功"):array(0,"添加失败");
		echo json_encode($str);
	}
	
	function menuedit($data){
		global $conf;
		$db = new db($conf);
		$id = isset($data["id"])?intval($data["id"]):0;
		$name = isset($data['t0'])?trim($data['t0']):"";
		$dir = isset($data['t1'])?trim($data['t1']):"";
		$menu = isset($data['t2'])?intval($data['t2']):0;
		$px = isset($data['t3'])?intval($data['t3']):0;
		$lx = isset($data['t4'])?intval($data['t4']):1;
		if($lx>3 || $lx<1){ error("栏目类型错误");}
		if(empty($name)){  error("请输入栏目名称");}
		if(empty($dir)){  error("请输入栏目目录");}
		$preg = "/\b[a-z]+\b/";
		if(!preg_match($preg,$dir)){ error("栏目目录请输入小写字母");}
		$r1 = $db->field(array("*"))->where(array("dirname"=>"'".$dir."'"))->select("sc_menu");
		if($r1 && $r1[0]['Id']!=$id){ error("该目录已存在");}
		$field = array("name"=>$name,"dirname"=>$dir,"menu"=>$menu,"lx"=>$lx,"px"=>$px);
		$result = $db->where(array("Id"=>$id))->update("sc_menu",$field);
		$str = $result?array(1,"修改成功"):array(0,"修改失败");
		echo json_encode($str);
	}
	
	function newsedit($data){
		global $conf;
		$db = new db($conf);
		$nid = isset($data['nid'])?intval($data['nid']):0;
		$title = isset($data['title'])?trim($data['title']):"";
		$cid = isset($data['menu2'])?intval($data['menu2']):0;
		$pic1 = isset($data['img'])?$data['img']:"";	
		$content = isset($data['content'])?htmlspecialchars($data['content']):"";
		if(!$nid){ error("通讯错误");}
		if(strlen($title)<2){ error("请输入文章标题");}
		if($cid<1){ error("请选择所属栏目");}		
		$cat = $db->field(array("*"))->where("Id=$cid")->select("sc_menu");
		if(!$cat){ error("所选栏目不存在");}
		$pic = '';
		if($pic1){
			$pic = $pic1[0];
		}
		$pic1 = json_encode($pic1);
		$r1 = $db->field(array("*"))->where("Id=$nid")->select("sc_news");
		if(!$r1){ error("通讯异常");}
		$xgls = date("Y-m-d H:i:s",time()).",".$r1[0]['xgls'];
		$db->where("Id=$nid")->update("sc_news",array("title"=>$title,"cid"=>$cid,"lx"=>$cat[0]['lx'],"pic"=>$pic,"pic1"=>$pic1,"content"=>$content,"state"=>1,"xgls"=>$xgls));
		success("修改成功");		
	}
	
	function newsadd($data){
		global $conf;
		$db = new db($conf);
		$title = isset($data['title'])?trim($data['title']):"";
		$cid = isset($data['menu2'])?intval($data['menu2']):0;
		$pic1 = isset($data['img'])?$data['img']:"";		
		$content = isset($data['content'])?htmlspecialchars($data['content']):"";
		if(strlen($title)<2){ error("请输入文章标题");}
		if($cid<1){ error("请选择所属栏目");}
		$cat = $db->field(array("*"))->where("Id=$cid")->select("sc_menu");
		if(!$cat){ error("所选栏目不存在");}
		$pic = '';
		if($pic1){
			$pic = $pic1[0];
		}
		$pic1 = json_encode($pic1);
		$db->insert("sc_news",array("title"=>$title,"cid"=>$cid,"lx"=>$cat[0]['lx'],"pic"=>$pic,"pic1"=>$pic1,"content"=>$content,"state"=>1,"addtime"=>date("Y-m-d H:i:s",time())));
		success("添加成功");		
	}
	
	function newsstate($data){
        global $conf;
		$db = new db($conf);
		$id = isset($data['id'])?intval($data['id']):0;
		if($id<=0){ error("操作错误");}
		$info = $db->field(array("*"))->where("Id=$id")->select("sc_news");
		if(!$info){ error('通讯异常');}
		if($info[0]['state']==1){
			error('该文章已审核通过 状态不可更改');
		    $db->where("Id=$id")->update("sc_news",['state'=>0]);
		    $str = [1,2,3,'审核中'];
		}else{
		    $db->where("Id=$id")->update("sc_news",['state'=>1]);			
		    $str = [1,3,2,'已发布'];			
		}
		echo json_encode($str);
    }

    public function infoadd($data){
		global $conf;
		$db = new db($conf);	
		$title = $_POST['t0'];
		$content = isset($_POST['editorValue'])?$_POST['editorValue']:"";
		if(empty($title)){
			$str = array(0,"请输入公告标题");echo json_encode($str);exit;
		}
		if(empty($content)){
			$str = array(0,"请输入该公告内容");echo json_encode($str);exit;
		}
		$field = array("title"=>$title,"content"=>$content,"state"=>1,"addtime"=>date("Y-m-d H:i:s",time()));
		$result = $db->insert("sc_info",$field);
		if($result){ $str = array(1,"添加成功");}
		else{ $str = array(0,"添加失败");}
		echo json_encode($str);
    }

	public function infoedit($data){
		global $conf;
		$db = new db($conf);
		$title = $_POST['t0'];
		$infoid = isset($_POST['infoid'])?intval($_POST['infoid']):0;
		$content = isset($_POST['editorValue'])?$_POST['editorValue']:"";
		if(empty($infoid)){
		$str = array(0,"系、信息异常，请刷新页面");echo json_encode($str);exit;
		}
		if(empty($title)){
		$str = array(0,"请输入公告标题");echo json_encode($str);exit;
		}
		if(empty($content)){
			$str = array(0,"请输入该公告内容");echo json_encode($str);exit;
		}
		$field = array("title"=>$title,"content"=>$content,"state"=>1,"addtime"=>date("Y-m-d H:i:s",time()));
		$result = $db->where(array("Id"=>$infoid))->update("sc_info",$field);
		if($result){ $str = array(1,"修改成功");}
		else{ $str = array(0,"修改失败");}
		echo json_encode($str);
    }

	public function infodel($data){		
		global $conf;
		$db = new db($conf);
		if(count($data)<=0){
			$str = array(0,"信息已更新，请刷新页面");
			echo json_encode($str);exit;
		}
		$id = $data['id'];
		$result = $db->where(array("Id"=>$id))->delete("sc_info");
		$str = $result?array(1,"删除成功"):array(0,"删除失败");
		echo json_encode($str);
    }
	
}